package com.ben.multiple.util;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * 文件复制，读取流信息
 *
 * @author ben
 * @date 2019/8/2
 */
public interface MyStreamUtils {

    /**
     * 读取流信息
     *
     * @param inputStream 输入流
     * @param charset     字符编码
     * @return String
     * @throws IOException IOException
     */
    static String getStreamContent(InputStream inputStream, Charset charset) throws IOException {
        if (inputStream == null) {
            return null;
        }
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int n;
        while ((n = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, n);
        }
        byte[] result = byteArrayOutputStream.toByteArray();
        inputStream.close();
        byteArrayOutputStream.close();
        return new String(result, charset);
    }

    static String getStreamContent(InputStream inputStream) throws IOException {
        return getStreamContent(inputStream, null);
    }

    /**
     * 读取文件
     *
     * @param url     文件路径
     * @param charset 字符编码
     * @return String
     * @throws IOException io
     */
    static String readFile(String url, Charset charset) throws IOException {
        if (url == null || "".equals(url)) {
            return null;
        }
        File file = new File(url);
        if (!file.isFile()) {
            return null;
        }
        charset = charset == null ? StandardCharsets.UTF_8 : charset;
        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int n;
        while ((n = fileInputStream.read(buff)) != -1) {
            byteArrayOutputStream.write(buff, 0, n);
        }
        byte[] bytes = byteArrayOutputStream.toByteArray();
        fileInputStream.close();
        byteArrayOutputStream.close();
        return new String(bytes, charset);
    }

    static String readFile(String url) throws IOException {
        return readFile(url, null);
    }

    /**
     * @param from        源文件 url
     * @param to          目标文件 url
     * @param fromCharset 源文件 编码格式
     * @param toCharset   目标文件 编码格式
     * @return File
     * @throws IOException IOException
     */
    static File copyFile(String from, String to, Charset fromCharset, Charset toCharset) throws IOException {
        if (from == null || "".equals(from)) {
            return null;
        }
        File fromFile = new File(from);
        if (!fromFile.isFile()) {
            return null;
        }
        if (to == null || "".equals(to)) {
            to = fromFile.getAbsolutePath() + "副本";
        }
        File toFile = new File(to);
        if (!toFile.isFile()) {
            boolean flag = toFile.createNewFile();
            if (!flag) {
                return null;
            }
        }
        if (fromCharset == null && toCharset == null) {
            fromCharset = StandardCharsets.UTF_8;
            toCharset = StandardCharsets.UTF_8;
        }
        if (fromCharset == null) {
            fromCharset = toCharset;
        }
        if (toCharset == null) {
            toCharset = fromCharset;
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile), fromCharset));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), toCharset));
        String line;
        while ((line = in.readLine()) != null) {
            writer.write(line);
            writer.newLine();
        }
        writer.close();
        in.close();
        return toFile;
    }

    static File copyFile(String from, String to, Charset fromCharset) throws IOException {
        return copyFile(from, to, fromCharset, null);
    }

    static File copyFile(String from, String to) throws IOException {
        return copyFile(from, to, null, null);
    }

    static File copyFile(String from, Charset fromCharset) throws IOException {
        return copyFile(from, null, fromCharset, null);
    }

    static File copyFile(String from) throws IOException {
        return copyFile(from, null, null, null);
    }

}
